//
//  CYFinanceDetailActionCell.m
//  cyjf
//
//  Created by tauCross on 2017/5/8.
//  Copyright © 2017年 Auto Financing. All rights reserved.
//

#import "CYFinanceDetailActionCell.h"



@implementation CYFinanceDetailActionCellObject

@end



@interface CYFinanceDetailActionCell ()

@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UILabel *subTitleLabel;
@property(nonatomic, strong)UIImageView *arrowImageView;
@property(nonatomic, strong)UIImageView *bottomLine;

@property(nonatomic, strong)UILabel *tagLabel;
@property(nonatomic, strong)CAShapeLayer *tagBorder;

@end


@implementation CYFinanceDetailActionCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self)
    {
        self.backgroundColor = [UIColor whiteColor];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = HEXCOLOR(@"444444");
        self.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:self.titleLabel];
        
        self.subTitleLabel = [[UILabel alloc] init];
        self.subTitleLabel.textColor = HEXCOLOR(@"999999");
        self.subTitleLabel.font = [UIFont systemFontOfSize:16];
        [self.contentView addSubview:self.subTitleLabel];
        
        self.arrowImageView = [[UIImageView alloc] initWithImage:CYImage(@"public_cell_arrow")];
        [self.contentView addSubview:self.arrowImageView];
        
        self.bottomLine = [[UIImageView alloc] initWithImage:[UIImage imageWithColor:HEXCOLOR(@"ECECEC") size:CGSizeMake(1, 1)]];
        [self.contentView addSubview:self.bottomLine];
        
        self.tagLabel = [[UILabel alloc] init];
        self.tagLabel.textColor = APP_DEFAULT_COLOR_RED;
        self.tagLabel.font = [UIFont systemFontOfSize:12 weight:UIFontWeightRegular];
        self.tagLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.tagLabel];
        
        self.tagBorder = [[CAShapeLayer alloc] init];
        self.tagBorder.strokeColor = APP_DEFAULT_COLOR_RED.CGColor;
        self.tagBorder.lineWidth = 1 / SCREEN_SCALE;
        self.tagBorder.fillColor = [UIColor clearColor].CGColor;
        [self.tagLabel.layer addSublayer:self.tagBorder];
    }
    return self;
}

+ (CGFloat)heightForObject:(id)object atIndexPath:(NSIndexPath *)indexPath tableView:(UITableView *)tableView
{
    return 50;
}

- (BOOL)shouldUpdateCellWithObject:(CYFinanceDetailActionCellObject *)object
{
    self.titleLabel.text = object.title;
    
    if(object.tag == nil || object.tag.length == 0)
    {
        self.subTitleLabel.text = object.subtitle;
        self.tagLabel.hidden = YES;
    }
    else
    {
        self.subTitleLabel.text = nil;
        self.tagLabel.hidden = NO;
        self.tagLabel.text = object.tag;
    }
    
    self.bottomLine.hidden = object.isBottom;
    
    [self setNeedsLayout];
    return YES;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.titleLabel sizeToFit];
    self.titleLabel.centerY = self.contentView.height / 2;
    self.titleLabel.left = 20;
    
    [self.subTitleLabel sizeToFit];
    self.subTitleLabel.centerY = self.contentView.height / 2;
    self.subTitleLabel.right = self.contentView.width - 37;
    
    self.arrowImageView.centerY = self.contentView.height / 2;
    self.arrowImageView.right = self.contentView.width - 12;
    
    self.bottomLine.width = self.contentView.width - 20;
    self.bottomLine.height = 1 / SCREEN_SCALE;
    self.bottomLine.left = 20;
    self.bottomLine.bottom = self.contentView.height;
    
    [self.tagLabel sizeToFit];
    self.tagLabel.height = 16;
    self.tagLabel.width += 14;
    self.tagLabel.centerY = self.contentView.height / 2;
    self.tagLabel.right = self.contentView.width - 39;
    self.tagBorder.path = [UIBezierPath bezierPathWithRoundedRect:self.tagLabel.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(5, 5)].CGPath;
    self.tagBorder.frame = self.tagLabel.bounds;
}

@end
